/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 晓星航
 * Date: 2023-10-11
 * Time: 8:43
 */
public class Matrix {
    public static void main(String[] args) {
        int n = 5; // 方阵大小
        int[][] matrix = new int[n][n]; // 创建一个n*n的二维数组作为方阵

        int num = 1; // 初始化填充数字
        int row = 0; // 初始化行坐标
        int col = n / 2; // 初始化列坐标

        while (num <= n * n) {
            matrix[row][col] = num;
            num++;

            int tempRow = row;
            int tempCol = col;

            // 判断下一个位置是否越界或已经被填充
            row--;
            col++;
            if (row < 0) {
                row = n - 1;
            }
            if (col > n - 1) {
                col = 0;
            }
            if (matrix[row][col] != 0) {
                row = tempRow + 1;
                col = tempCol;
                if (row > n - 1) {
                    row = 0;
                }
            }
        }

        // 打印方阵
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(matrix[i][j] + "\t");
            }
            System.out.println();
        }
    }
}
